package cn.tx.service.impl;

import cn.tx.entity.PageResult;
import cn.tx.entity.QueryPageBean;
import cn.tx.mapper.CheckGroupDao;
import cn.tx.mapper.CheckItemDao;
import cn.tx.pojo.CheckGroup;
import cn.tx.pojo.CheckItem;
import cn.tx.pojo.InterTable;
import cn.tx.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Snake Eyes
 * @PROJECT_NAME: health_parent
 * @DESCRIPTION:
 * @USER: 11647
 * @DATE: 2021/8/27 12:52
 * @Email: 1164763541@qq.com
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao groupDao;
    @Autowired
    private CheckItemDao itemDao;

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> groups = groupDao.findAll();
        return groups;
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        //获取参数
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //创建查询
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = groupDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public void save(Integer[] checkitemIds, CheckGroup formData) {
        //新增检查组
        groupDao.save(formData);
        //新增检查项t_checkgroup_checkitem
        HashMap<String, Integer> map = new HashMap<>(checkitemIds.length);
        for (Integer checkitemId : checkitemIds) {
            map.put("checkgroup_id", formData.getId());
            map.put("checkitem_id", checkitemId);
            groupDao.saveCheckGroupId_CheckItemId(map);

        }
    }

    /**
     * 通过检查组查询对应的检查像
     *
     * @param checkGroup
     * @return
     */
    @Override
    public List<Integer> checkItemFindById(Integer checkGroup) {
//        通过检查组查询对应的检查项目  返回的是对象
        List<InterTable> checkItemIds = groupDao.checkItemFindById(checkGroup);
        List<Integer> items = new ArrayList<>();
        for (InterTable checkItemId : checkItemIds) {
            items.add(checkItemId.getCheckItemId());
        }

        return items;
    }

    /**
     * @return
     * @author :11647
     * @date ：Created in 2021/8/28  16:03
     * @description：(描述) 修改方法
     * @modified By：11647
     * @version:
     */
    @Override
    public void update(Integer[] checkitemIds, CheckGroup formData) {
//        修改检查组信息
        groupDao.update(formData);
        //删除原有表关系
        groupDao.deleteCheckGroupId_CheckItemId(formData.getId());
        //数据封装
        HashMap<String,Integer> map = new HashMap();
        //修改检查项信息
        for (Integer checkitemId : checkitemIds) {
            map.put("checkgroup_id", formData.getId());
            map.put("checkitem_id", checkitemId);
            groupDao.saveCheckGroupId_CheckItemId(map);

        }
    }
/**
 * @author     :11647
 * @date       ：Created in 2021/8/28  18:59
 * @description：(描述) 删除方法
 * @modified By：11647
 * @version:
 * @return
 */
    @Override
    public void delete(Integer checkGroupId) {
        //删除一对多表
        groupDao.deleteCheckGroupId_CheckItemId(checkGroupId);
        //删除检查组
        groupDao.deleteFindById(checkGroupId);
    }
}
